/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.unipar.oficina.listener;

import javax.faces.application.FacesMessage;
import javax.faces.application.FacesMessage.Severity;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;
import org.springframework.security.authentication.AuthenticationCredentialsNotFoundException;
import org.springframework.security.authentication.AuthenticationServiceException;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.web.WebAttributes;
import org.springframework.security.web.authentication.session.SessionAuthenticationException;
import org.springframework.transaction.CannotCreateTransactionException;

/**
 *
 * @author Thiago Marques
 */
public class LoginErrorPhaseListener implements PhaseListener {

    private static final long serialVersionUID = 23L;

    @Override
    public void afterPhase(PhaseEvent phaseEvent) {
    }

    @Override
    public void beforePhase(PhaseEvent phaseEvent) {
        ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
        Exception exception = (Exception) context.getSessionMap().get(WebAttributes.AUTHENTICATION_EXCEPTION);
        if (exception != null) {
            context.getSessionMap().put(WebAttributes.AUTHENTICATION_EXCEPTION, null);
            if (exception instanceof AuthenticationServiceException) {
                mensagem("btnEntrar", "Sistema fora do ar para manutenção", FacesMessage.SEVERITY_INFO);
            } else if (exception instanceof CannotCreateTransactionException) {
                mensagem("btnEntrar", "Sistema fora do ar para manutenção", FacesMessage.SEVERITY_INFO);
            } else if (exception instanceof BadCredentialsException) {
                mensagem("btnEntrar", "Usuário e/ou senha errados", FacesMessage.SEVERITY_ERROR);
            } else if (exception instanceof SessionAuthenticationException) {
                mensagem("btnEntrar", "Este usuário já está sendo utilizado", FacesMessage.SEVERITY_ERROR);
            } else if (exception instanceof DisabledException) {
                mensagem("btnEntrar", "Esse usuário está desabilitado", FacesMessage.SEVERITY_ERROR);
            } else if (exception instanceof AuthenticationCredentialsNotFoundException) {
                mensagem("btnEntrar", "Esse usuário não está cadastrado", FacesMessage.SEVERITY_ERROR);
            } else if (exception instanceof Exception) {
                mensagem("btnEntrar", "Sistema fora do ar para manutenção", FacesMessage.SEVERITY_INFO);
            }
        }
    }
    
    private void mensagem(String clientId, String mensagem, Severity severity) {
        FacesContext context = FacesContext.getCurrentInstance();
        FacesMessage message = new FacesMessage(severity, mensagem, null);
        context.addMessage(clientId, message);
    }

    @Override
    public PhaseId getPhaseId() {
        return PhaseId.RENDER_RESPONSE;
    }
}